package com.youdianstar.app.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import coil.load

import com.youdianstar.app.common.ConfigKey
import com.youdianstar.app.common.ex.setThrottleListener
import com.youdianstar.app.common.widget.RadiusImageView
import com.youdianstar.app.R
import com.youdianstar.app.util.DataAppManager


/**
 * File: ShopImgAdapter
 * Author: 82149 Create: 2022/3/28 17:12
 * Changes (from 2022/3/28)
 * --------------------------------------------------
 * description: 网点图片
 * ---------------------------------------------------
 *
 */
class ShopImgAdapter(val context: Context,val datas: MutableList<String>,val itemClick: ((Int) -> Unit) = {}) :RecyclerView.Adapter<ShopImgAdapter.PicHolder>() {
    class PicHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        val ivIcon = itemView.findViewById<RadiusImageView>(R.id.iv_shop)
    }

    private var host = ""
    init {
        this.host = DataAppManager.instance.getConfig(ConfigKey.FILE_HOST)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PicHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.layout_image_shop, parent, false)
        return PicHolder(view)
    }

    override fun onBindViewHolder(holder: PicHolder, position: Int) {
        datas[position].let {
            val pathStr = if(it.contains("http")) datas[position] else host+datas[position]
            holder.ivIcon.load(pathStr){
                error(R.drawable.ic_img_picker_default)
            }
        }

        holder.ivIcon.setThrottleListener {
            itemClick.invoke(position)
        }
    }

    override fun getItemCount(): Int {
        return datas.size //if (datas.size>3) 3 else datas.size
    }

    /**刷新数据*/
    fun refreshData(list: List<String>) {
        datas.clear()
        datas.addAll(list)

        notifyDataSetChanged()
    }
}